package pl.iachoo.ctl.tabory.dao.ibatis.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import pl.iachoo.ctl.tabory.dao.api.services.IUserService;
import pl.iachoo.ctl.tabory.dao.data.vo.UserVO;
import pl.iachoo.ctl.tabory.dao.ibatis.services.BaseDaoService;

import com.ibatis.sqlmap.client.SqlMapClient;

public class UserDaoServiceImpl extends BaseDaoService implements IUserService {

	private final Logger logger = Logger.getLogger(UserDaoServiceImpl.class);

	private final static String MAPPER = "map-user";
	private final static String SQL_SELECT_BY_LOGON = ".selectUserByLogon";
	private final static String SQL_SELECT_BY_ID = ".selectUserById";
	private final static String SQL_SELECT_ALL = ".selectAllUsers";
	private final static String SQL_DELETE = ".deleteUser";
	private final static String SQL_SAVE = ".saveUser";
	private final static String SQL_UPDATE = ".updateUser";

	public UserDaoServiceImpl(SqlMapClient sqlMapClient) {
		super(sqlMapClient);
	}

	@Override
	public UserVO getUser(String userLogon) throws Exception {
		logger.debug("Getting user for logon: " + userLogon);
		sqlMapClient.startTransaction();
		UserVO user = (UserVO) sqlMapClient.queryForObject(MAPPER
				+ SQL_SELECT_BY_LOGON);
		sqlMapClient.commitTransaction();
		logger.debug(user);
		return user;
	}

	@Override
	public UserVO getUser(Long userId) throws Exception {
		logger.debug("Getting user for id: " + userId);
		sqlMapClient.startTransaction();
		UserVO user = (UserVO) sqlMapClient.queryForObject(MAPPER
				+ SQL_SELECT_BY_ID);
		sqlMapClient.commitTransaction();
		logger.debug(user);
		return user;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserVO> getAllUsers() throws Exception {
		logger.debug("Getting all users");
		sqlMapClient.startTransaction();
		List<UserVO> list = sqlMapClient.queryForList(MAPPER + SQL_SELECT_ALL);
		sqlMapClient.commitTransaction();
		logger.debug(list);
		return list;
	}

	@Override
	public UserVO saveUpdateUser(UserVO user) throws Exception {
		logger.debug("Save/Update user: " + user);
		if (user.getId() == null) {
			sqlMapClient.insert(MAPPER + SQL_SAVE, remapUser(user));
		} else {
			sqlMapClient.update(MAPPER + SQL_UPDATE, remapUser(user));
		}
		return null;
	}

	@Override
	public void deleteUser(UserVO user) throws Exception {
		logger.debug("Deleting user: " + user);
		sqlMapClient.startTransaction();
		sqlMapClient.delete(MAPPER + SQL_DELETE, user.getId());
		sqlMapClient.commitTransaction();
		logger.debug("Delete OK");
	}

	private Map<String, Object> remapUser(UserVO user) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", user.getId());
		map.put("dateValidFrom", user.getDateValidFrom());
		map.put("dateValidUntil", user.getDateValidUntil());
		map.put("userLogon", user.getUserLogon());
		map.put("userName", user.getUserName());
		map.put("userPass", user.getUserPass());
		map.put("userRole", user.getUserRoleVO().getId());
		map.put("active", user.isActive());
		return map;
	}

}
